<?php
/**
 * @version		0.2.0 joomla zmluvy $
 * @package		com_zjmluvy
 * @copyright	Copyright © 2011 - All rights reserved.
 * @license		GNU/GPL
 * @author    AdrianKo		
 * @website   http://code.google.com/p/joomla-zmluvy
 *
 *
 *1.3.2012
 */

// no direct access
defined('_JEXEC') or die('Restricted access');

jimport( 'joomla.application.component.view' );
jimport( 'joomla.filesystem.file' );



class jzmluvyViewFaktura extends Jview
{
	function display($tpl = null)
  {
    global $mainframe, $option;

		$db				=& JFactory::getDBO();
		$document 	= & JFactory::getDocument();
		$user 		= & JFactory::getUser();
    $document->addStyleDeclaration('p {margin: 2px 0px;}');

		$context = 'com_jzmluvyfaktura.list.';
		$params	= &JComponentHelper::getParams('com_jzmluvy');
    $nastavenie = & jzmluvyHelper::nastavenie();
    		
    $limit		= $mainframe->getUserStateFromRequest( 'global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int' );
		$limitstart = $mainframe->getUserStateFromRequest( $context.'limitstart', 'limitstart', 0, 'int' );
    		
		$filter_order		= $mainframe->getUserStateFromRequest( $context.'filter_order',		'filter_order',		'ud.publish_up',	'cmd' );
		$filter_order_Dir	= $mainframe->getUserStateFromRequest( $context.'filter_order_Dir',	'filter_order_Dir',	'DESC',			'word' );

    $search				= $mainframe->getUserStateFromRequest( $context.'search',			'search', 			'',			'string' );
    
		$filter_kategorie		= $mainframe->getUserStateFromRequest( $context.'filter_kategorie',		'filter_kategorie', 		'',			'string' );
		$filter_archiv		= $mainframe->getUserStateFromRequest( $context.'filter_archiv',		'filter_archiv', 	'',			'string' );
		$filter_anotace		= $mainframe->getUserStateFromRequest( $context.'filter_anotace',		'filter_anotace', 	'1',			'string' );

    

		
		// nahratie submenu
    require (JPATH_COMPONENT_ADMINISTRATOR.DS.'admin.submenu.php');
    		
		if (strpos($search, '"') !== false) {
			$search = str_replace(array('=', '<'), '', $search);
		}
		$search = JString::strtolower($search);
		
		$orderby = ' ORDER BY '. $filter_order .' '. $filter_order_Dir;
		
		$where = array();
		if (isset( $search ) && $search!= '')
		{
			$searchEscaped = $db->Quote( '%'.$db->getEscaped( $search, true ).'%', false );
			$where[] = 'ud.title LIKE '.$searchEscaped.' OR ud.fulltext LIKE '.$searchEscaped;
		}
		if ($filter_archiv != '')
		{
      if ( $filter_archiv == '0' )
			{
				$where[] = ' ud.state = 0 ';
			}
			else if ( $filter_archiv == '-1' )
			{
				$where[] = ' ud.state = -1 ';
			}
		}
		/* natvrdo nastavena kategoria faktura
    if ($filter_kategorie != '')
		{
			$where[] = ' ud.catid = '.$filter_kategorie;
		}
		*/
    
    $where[] = ' ud.catid = 1';
		$where[] = 'k.published = 1 || ud.catid = 0';
		$where = ( count( $where ) ? ' WHERE (' . implode( ') AND (', $where ) . ')' : '' );
		
		
		$query = 'SELECT COUNT(ud.id)'
		  . ' FROM #__jzmluvy AS ud'
		  . ' LEFT JOIN #__jzmluvy_kategorie AS k ON ud.catid = k.id'
		  . $where
		;
		$db->setQuery( $query );
		$total = $db->loadResult();
    
    jimport('joomla.html.pagination');
		$pagination = new JPagination( $total, $limitstart, $limit );
		
		$query = 'SELECT ud.*, k.title AS catitle, f.title AS fadod, u.name AS username, ko.name AS kontakt, v.faktura AS typ_zml, q.faktura AS typ_obj, v.fulltext AS zml_text, q.title AS obj_text'
			. ' FROM #__jzmluvy AS ud'
			. ' LEFT JOIN #__jzmluvy AS v ON ud.typ = v.id'
      . ' LEFT JOIN #__jzmluvy AS q ON ud.sposob = q.id'
      . ' LEFT JOIN #__jzmluvy_kategorie AS k ON ud.catid = k.id'
      . ' LEFT JOIN #__jzmluvy_partner AS f ON ud.dodavatel = f.id'
			. ' LEFT JOIN #__users AS u ON ud.created_by = u.id'
			. ' LEFT JOIN #__contact_details AS ko ON ud.created_by_alias = ko.id'
			. $where
			. $orderby
		;

		$db->setQuery( $query, $pagination->limitstart, $pagination->limit );
		$rows = $db->loadObjectList();

    $archiv[] = JHTML::_('select.option',  '', '- Všechny záznamy -');
    $archiv[] = JHTML::_('select.option',  0, 'Aktuální');
		$archiv[] = JHTML::_('select.option',  -1, 'Archiv');
		$lists['archiv'] = JHTML::_('select.genericlist',   $archiv, 'filter_archiv', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', "$filter_archiv" );
		
		$anotace[] = JHTML::_('select.option',  1, 'Zobrazit anotaci');
    $anotace[] = JHTML::_('select.option',  0, 'Skrýt anotaci');
		$lists['anotace'] = JHTML::_('select.genericlist',   $anotace, 'filter_anotace', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', "$filter_anotace" );
		
		/* zrusenie vyberu kategorie 
    $query = 'SELECT *'
			. ' FROM #__jzmluvy_kategorie'
			. ' WHERE published = 1'
		;

		$db->setQuery( $query );		
		$kategorie[] 		= JHTML::_('select.option',  '', '- Zvolte kategorii -' );
    foreach( $db->loadObjectList() as $obj )
		{
			$kategorie[] = JHTML::_('select.option',  $obj->id, $obj->title );
		}
		$lists['kategorie'] 	= JHTML::_('select.genericlist',   $kategorie, 'filter_kategorie', 'class="inputbox" size="1" onchange="document.adminForm.submit( );"', 'value', 'text', "$filter_kategorie" );
		 */
     
		// table ordering
		$lists['order_Dir']	= $filter_order_Dir;
		$lists['order']		= $filter_order;
		
		// search filter
		$lists['search']= $search;
		
		$this->assignRef('items',		$rows);
		$this->assignRef('lists',		$lists);
		$this->assignRef('anotace',		$filter_anotace);
		$this->assignRef('pagination',	$pagination);
		$this->assignRef('params', $params);
    $this->assignRef( 'nastavenie', $nastavenie );
    
		parent::display($tpl);

	}

  //*** zadefinovanie sposobu uhrady
	function sp_uhrady ($typ) {
     if ( $typ == 1 ) { 
     $output = JText::_( 'fa_EDIT_SPOSOB_UHRAD_BEZHOTOVOSTNE' );
      }
      else if ( $typ == 2 ) { 
      $output = JText::_( 'FA_EDIT_SPOSOB_UHRAD_VHOTOVOSTI' ); 
     }
      return $output;
      }

      
	function soubor ($id,$file,$filename,$extension) {
//    $params = JComponentHelper::getParams ('com_jzmluvy');
//    $dir = 'components/com_jzmluvy/files/'; 
//    $nazevSouboru = JFile::stripExt($file);
//    $priponaSouboru = JFile::getExt($file);
    $dir = JPATH_ROOT . DS . $this->params->get( 'adresar');
    $nazevSouboru = $id.'-'.$filename.'.'.$extension;
    //*** ak je subor poskodeny alebo zmazany
    if (file_exists($dir.$nazevSouboru)) { 
    //*** zobrazenie ikony a velkosti suboru
    $filesize = filesize($dir.$nazevSouboru);    
    $output = '<a href="../'.$this->params->get( 'adresar').$nazevSouboru.'" target="'.$this->params->get( 'link', '_blank').'">'.jzmluvyHelper::ikona ($extension,$file).'</a>';
    //*** $output .= ' ['.jzmluvyHelper::format_bytes($filesize).']';
    return $output;  
    }
    //*** ak subor neexistuje zobraz chybovu hlasku
    else
    {
    $this->_error = JText::_( 'FA_EDIT_CHYBA_SUBOR_POSKODENY_SCAN').': '.$id;
	  JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	  $output = '<a href="index.php?option=com_jzmluvy&view=upravafaktury&hidemainmenu=1&task=edit&id[]='.$id.'">';
    $output .= '<img src="components/com_jzmluvy/assets/images/icon16/file_broken.png" align="middle" title="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" alt="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" /></a>';
    return $output;
    }  
	}

  	function soubor2 ($id,$file2,$filename2,$extension2) {
//    $params = JComponentHelper::getParams ('com_jzmluvy');
//    $dir = 'components/com_jzmluvy/files/'; 
//    $nazevSouboru = JFile::stripExt($file);
//    $priponaSouboru = JFile::getExt($file);
    $dir = JPATH_ROOT . DS . $this->params->get( 'adresar');
    $nazevSouboru = $id.'-'.$filename2.'.'.$extension2;
    //*** ak je subor poskodeny alebo zmazany
    if (file_exists($dir.$nazevSouboru)) { 
    //*** zobrazenie ikony a velkosti suboru
    $filesize = filesize($dir.$nazevSouboru);
    $output = '<a href="../'.$this->params->get( 'adresar').$nazevSouboru.'" target="'.$this->params->get( 'link', '_blank').'">'.jzmluvyHelper::ikona ($extension2,$file2).'</a>';
    //*** $output .= ' ['.jzmluvyHelper::format_bytes($filesize).']';
    return $output;
    }
    //*** ak subor neexistuje zobraz chybovu hlasku
    else
    {
    $this->_error = JText::_( 'FA_EDIT_CHYBA_SUBOR_POSKODENY_TEXT').': '.$id;
	  JError::raiseWarning('SOME_ERROR_CODE', $this->_error );
	  $output = '<a href="index.php?option=com_jzmluvy&view=upravafaktury&hidemainmenu=1&task=edit&id[]='.$id.'">';
    $output .= '<img src="components/com_jzmluvy/assets/images/icon16/file_broken.png" align="middle" title="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" alt="'.JText::_( 'FA_EDIT_SUBOR_POSKODENY').'" /></a>';
    return $output;
    }  
	}

}
?>